<?php

namespace app\rental\controller;

use app\rental\model\RentalPriceRangeModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminTagController 价格区间管理控制器
 * @package app\rental\controller
 */
class AdminPriceRangeController extends AdminBaseController
{
    /**
     * 价格区间管理
     * @adminMenu(
     *     'name'   => '价格区间',
     *     'parent' => 'rental/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 3,
     *     'icon'   => '',
     *     'remark' => '价格区间',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $rentalPriceRangeModel = new RentalPriceRangeModel();
        $prices           = $rentalPriceRangeModel->order('min_price  asc')->paginate();
        $this->assign("prices", $prices);
        $this->assign('page', $prices->render());
        return $this->fetch();
    }

    /**
     * 添加价格区间
     * @adminMenu(
     *     'name'   => '添加价格区间',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加价格区间',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $rentalPriceRangeModel = new RentalPriceRangeModel();
        return $this->fetch();
    }

    /**
     * 添加价格区间提交
     * @adminMenu(
     *     'name'   => '添加价格区间提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加价格区间提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $arrData = $this->request->param();

        $rentalPriceRangeModel = new RentalPriceRangeModel();
        $rentalPriceRangeModel->isUpdate(false)->allowField(true)->save($arrData);

        $this->success(lang("SAVE_SUCCESS"),url('AdminPriceRange/index'));

    }
    /**
     * 编辑价格区间
     * @adminMenu(
     *     'name'   => '编辑价格区间',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑价格区间',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $rentalPriceRangeModel = new RentalPriceRangeModel();
            $price = $rentalPriceRangeModel->get($id)->toArray();
            $this->assign("price", $price);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }

    }
    public function editPost()
    {
        $intId     = $this->request->param("id");
        $intMaxPrice = $this->request->param("max_price");
        $intMinPrice = $this->request->param("min_price");
        if (empty($intId)) {
            $this->error('操作错误');
        }
        $rentalPriceRangeModel = new RentalPriceRangeModel();
        $rentalPriceRangeModel->isUpdate(true)->save(["min_price" => $intMinPrice,"max_price" => $intMaxPrice], ["id" => $intId]);
        $this->success('保存成功',url('AdminPriceRange/index'));
    }

    /**
     * 删除价格区间
     * @adminMenu(
     *     'name'   => '删除价格区间',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '删除价格区间',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $rentalPriceRangeModel = new RentalPriceRangeModel();

        $rentalPriceRangeModel->where(['id' => $intId])->delete();

        $this->success(lang("DELETE_SUCCESS"));
    }
}
